<template>

  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入模板标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片地址" prop="thumbnailsUrl">
        <el-input
          v-model="queryParams.thumbnailsUrl"
          placeholder="请输入图片地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属系统" prop="systemId">
        <el-input
          v-model="queryParams.systemId"
          placeholder="请输入所属系统"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="typeId">
        <el-input
          v-model="queryParams.typeId"
          placeholder="请输入设备类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属客户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入所属客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属店铺" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入所属店铺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createUserId">
        <el-input
          v-model="queryParams.createUserId"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="dateCreated">
        <el-date-picker clearable
          v-model="queryParams.dateCreated"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tagtemplates:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tagtemplates:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tagtemplates:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tagtemplates:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tagtemplatesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板ID" align="center" prop="templateId" />
      <el-table-column label="模板标题" align="center" prop="title" />
      <el-table-column label="模板文件URL" align="center" prop="jsonUrl" />
      <el-table-column label="模板JSON" align="center" prop="jsonContent" />
      <el-table-column label="图片地址" align="center" prop="thumbnailsUrl" />
      <el-table-column label="模板背景" align="center" prop="background" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.background" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="所属系统" align="center" prop="systemId" />
      <el-table-column label="设备类型" align="center" prop="typeId" />
      <el-table-column label="所属客户" align="center" prop="userId" />
      <el-table-column label="所属店铺" align="center" prop="storeId" />
      <el-table-column label="创建者" align="center" prop="createUserId" />
      <el-table-column label="创建日期" align="center" prop="dateCreated" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateCreated, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleDesign(scope.row)"
            v-hasPermi="['system:tagtemplates:design']"
          >设计</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-brush"
            @click="handleMaketag(scope.row)"
            v-hasPermi="['system:tagtemplates:maketag']"
          >生成标签</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:tagtemplates:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tagtemplates:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模板信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1800" append-to-body>
      <el-row>
        <el-col :span="24">
          <div class="flex-col">
            <div class="flex-row justify-center" style="margin-bottom: 10px">
              <!-- 纸张大小 A3、A4 等 -->
              <div class="paper">

                <!-- 自定义纸张 -->
                <button :class="'other' == curPaperType ? 'api' : 'info'" class="auto" @click="showPaperPop">自定义纸张</button>
                <div class="popover">
                  <div class="popover-content flex-col" v-show="paperPopVisible">
                    <div style="font-size: 16px; font-weight: bold">设置纸张宽高(mm)</div>
                    <div class="flex-row mt-10">
                      <input class="input" :value="paperWidth" type="number" placeholder="宽(mm)" />
                      <span class="ml-10 mr-10">x</span>
                      <input class="input" :value="paperHeight" type="number" placeholder="高(mm)" />
                    </div>
                    <button class="primary circle-10" style="margin-top: 6px" @click.stop="setPaperOther">确定</button>
                  </div>
                </div>
              </div>
              <!-- 缩放 -->
              <div class="flex-row align-center ml-10">
                <button class="info circle-10" @click="changeScale(false)"><i class="iconfont sv-zoom-out" /></button>
                <div style="margin: 0 4px; width: 40px">{{ (scaleValue * 100).toFixed(0) }}%</div>
                <button class="info circle-10" @click="changeScale(true)"><i class="iconfont sv-zoom-in" /></button>
              </div>
              <button class="api circle-10 ml-10" @click.stop="rotatePaper">
                <i class="iconfont sv-rotate" />
                旋转纸张(宽高互换)
              </button>
              <button class="api circle-10 ml-10" @click.stop="clearPaper">
                <i class="iconfont sv-clear" />
                清空纸张
              </button>
              <button class="api circle-10 ml-10" @click.stop="exportJson">
                <i class="iconfont sv-export" />
                导出模板 json
              </button>
              <button class="api circle-10 ml-10" @click.stop="exportJsonTid">
                导出模板 json tid
                <i class="iconfont sv-export" />
              </button>
              <button class="secondary circle-10 ml-10" @click.stop="print">
                <i class="iconfont sv-printer" />
                浏览器打印
              </button>
              <button class="warning circle-10 ml-10" @click.stop="print2">
                直接打印(需要连接客户端)
                <i class="iconfont sv-printer" />
              </button>
            </div>
            <div class="flex-row" style="height: 87vh">
              <div class="flex-2 left flex-col">
                <div class="title">provider1 默认样式</div>
                <!-- provider1 的容器; 加上 class "rect-printElement-types" 使用默认样式 -->
                <!-- 当然可以 重写 或者 自定义样式 -->
                <div id="provider-container1" class="container rect-printElement-types"></div>
                <div class="title">provider2 自定义样式</div>
                <!-- provider2 的容器; -->
                <!-- 这里自定义显示样式 custom-style-types -->
                <div id="provider-container2" class="container custom-style-types"></div>
                <div class=""></div>
              </div>
              <div class="flex-5 center">
                <!-- 设计器的 容器 -->
                <div id="hiprint-printTemplate"></div>
              </div>
              <div class="flex-2 right">
                <!-- 元素参数的 容器 -->
                <div id="PrintElementOptionSetting"></div>
              </div>
            </div>
          </div>
        </el-col>

      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入模板标题" />
        </el-form-item>
        <el-form-item label="模板文件URL" prop="jsonUrl">
          <file-upload v-model="form.jsonUrl"/>
        </el-form-item>
        <el-form-item label="模板JSON">
          <editor v-model="form.jsonContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="图片地址" prop="thumbnailsUrl">
          <el-input v-model="form.thumbnailsUrl" placeholder="请输入图片地址" />
        </el-form-item>
        <el-form-item label="模板背景" prop="background">
          <image-upload v-model="form.background"/>
        </el-form-item>
        <el-form-item label="所属系统" prop="systemId">
          <el-input v-model="form.systemId" placeholder="请输入所属系统" />
        </el-form-item>
        <el-form-item label="设备类型" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入设备类型" />
        </el-form-item>
        <el-form-item label="所属客户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入所属客户" />
        </el-form-item>
        <el-form-item label="所属店铺" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入所属店铺" />
        </el-form-item>
        <el-form-item label="创建者" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建日期" prop="dateCreated">
          <el-date-picker clearable
            v-model="form.dateCreated"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>

/* 定义全局样式 */
/* 在 main.js 入库文件引入 */
body {
  margin: 0;
}

/* scrollbar */
::-webkit-scrollbar {
  height: 4px;
  width: 4px;
}
::-webkit-scrollbar-corner {
  height: 4px;
  width: 4px;
}
::-webkit-scrollbar-thumb {
  background: purple;
  border-radius: 2px;
  background-image: -webkit-linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.2) 25%,
    transparent 25%,
    transparent 50%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0.2) 75%,
    transparent 75%,
    transparent
  );
}
::-webkit-scrollbar-thumb:hover {
  background: purple;
}

/* flex */
.flex-row {
  display: flex;
}
.flex-col {
  display: flex;
  flex-direction: column;
}
.flex-wrap {
  flex-wrap: wrap;
}
.align-center {
  align-items: center;
}
.justify-center {
  justify-content: center;
}

.flex-1 {
  flex: 1;
}
.flex-2 {
  flex: 2;
}
.flex-3 {
  flex: 3;
}
.flex-4 {
  flex: 4;
}
.flex-5 {
  flex: 5;
}

.ml-10 {
  margin-left: 10px;
}
.mr-10 {
  margin-right: 10px;
}
.mt-10 {
  margin-top: 10px;
}
.mb-10 {
  margin-bottom: 10px;
}

/* button 样式 为了好看点 */
button {
  padding: 10px;
  min-width: 40px;
  color: white;
  opacity: 0.9;
  cursor: pointer;
  border-width: 0;
  border: 1px solid #d9d9d9;
}
button:hover {
  opacity: 1;
}
button i {
  font-size: 16px !important;
}
.circle,
.circle-4 {
  border-radius: 4px !important;
}
.circle-10 {
  border-radius: 10px !important;
}
/* 按钮颜色 */
.primary {
  background: purple;
}
.info {
  color: #000;
  background: none;
}
.info:hover {
  color: purple;
  border-color: purple;
}
.secondary {
  background: #1976d2;
}
.warning {
  background: #d32f2f;
}

/* modal */
.modal {
  padding: 0;
  margin: 0;
}
.modal .mask {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  height: 100%;
  background-color: #00000073;
}
.modal .wrap {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  overflow: auto;
  background-color: #00000073;
  outline: 0;
}
.modal .wrap .box {
  position: relative;
  margin: 10% auto;
  width: 40%;
  background: #fff;
  border-radius: 4px;
  z-index: 1001;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}
.modal-box__header {
  padding: 10px 14px;
  border-bottom: 1px solid #e9e9e9;
}
.modal-box__footer {
  text-align: end;
}
.modal-box__footer button {
  min-width: 100px;
}
.modal-box__footer button:not(:last-child) {
  margin-right: 10px;
}

/*全局结束*/


   /* 重写全局 hiprint 样式 */
 .hiprint-headerLine,
 .hiprint-footerLine {
   border-color: purple !important;
 }

.hiprint-headerLine:hover,
.hiprint-footerLine:hover {
  border-top: 3px dashed purple !important;
}

.hiprint-headerLine:hover:before {
  content: "页眉线";
  left: calc(50% - 18px);
  position: relative;
  background: #ffff;
  top: -14px;
  color: purple;
  font-size: 12px;
}

.hiprint-footerLine:hover:before {
  content: "页脚线";
  left: calc(50% - 18px);
  position: relative;
  color: purple;
  background: #ffff;
  top: -14px;
  font-size: 12px;
}

/* 区域 */
.left {
  background: white;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  padding: 10px 0;
  box-shadow: 2px 2px 2px 0px rgb(128 0 128 / 20%);
  overflow: auto;
}
.center {
  margin: 0 10px;
  background: white;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  padding: 20px;
  box-shadow: 2px 2px 2px 0px rgb(128 0 128 / 20%);
  overflow: auto;
}
.right {
  background: white;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  padding: 10px 0;
  box-shadow: 2px 2px 2px 0px rgb(128 0 128 / 20%);
  overflow: auto;
}
/* 左侧拖拽元素样式 */
.title {
  font-size: 16px;
  font-weight: 500;
  width: 100%;
  margin: 10px 0 0 24px;
}
.item {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: white;
  padding: 4px 10px;
  margin: 10px 8px 4px 8px;
  width: 38%;
  min-height: 60px;
  border-radius: 4px;
  box-shadow: 2px 2px 2px 2px rgba(171, 171, 171, 0.2);
}
.item .iconfont {
  font-size: 1.5rem;
}
.item span {
  font-size: 14px;
}
</style>
<script>
import { listTagtemplates, getTagtemplates, delTagtemplates, addTagtemplates, updateTagtemplates } from "@/api/system/tagtemplates";

import { hiprint, defaultElementTypeProvider } from "vue-plugin-hiprint";
import { provider1 } from "@/provider1";
import template from "@/tagtemplatedata";

// 初始化 provider
hiprint.init({
  providers: [defaultElementTypeProvider()],
});

/**
 * 构建左侧可拖拽元素
 * 注意: 可拖拽元素必须在 hiprint.init() 之后调用
 * 而且 必须包含 class="ep-draggable-item" 否则无法拖拽进设计器
 */
const buildLeftElement = () => {
  // eslint-disable-next-line no-undef
  hiprint.PrintElementTypeManager.buildByHtml($(".ep-draggable-item"));
};
/**
 * 构建设计器
 * 注意: 必须要在 onMounted 中去构建
 * 因为都是把元素挂载到对应容器中, 必须要先找到该容器
 */
let hiprintTemplate;
const buildDesigner = () => {
  // eslint-disable-next-line no-undef
  $("#hiprint-printTemplate").empty(); // 先清空, 避免重复构建
  hiprintTemplate = new hiprint.PrintTemplate({
    settingContainer: "#PrintElementOptionSetting", // 元素参数容器
  });
  // 构建 并填充到 容器中
  // 可以先 console.log($("#hiprint-printTemplate")) 看看是否有该 dom
  hiprintTemplate.design("#hiprint-printTemplate");
};


export default {
  name: "Tagtemplates",
  data() {
    return {
      // hiprint
      hiprintTemplate: null,
      elements: [
        { title: '文本', type: 'text' },
        { title: '表格', type: 'table' },
        { title: '线条', type: 'hline' }
      ],
      templateId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模板信息表格数据
      tagtemplatesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        jsonUrl: null,
        jsonContent: null,
        thumbnailsUrl: null,
        background: null,
        systemId: null,
        typeId: null,
        userId: null,
        storeId: null,
        createUserId: null,
        dateCreated: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "模板标题不能为空", trigger: "blur" }
        ],
        jsonContent: [
          { required: true, message: "模板JSON不能为空", trigger: "blur" }
        ],
      }
    };
  },
  mounted() {
    buildLeftElement();
    buildDesigner();
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板信息列表 */
    getList() {
      this.loading = true;
      listTagtemplates(this.queryParams).then(response => {
        this.tagtemplatesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        templateId: null,
        title: null,
        jsonUrl: null,
        jsonContent: null,
        thumbnailsUrl: null,
        attributes: null,
        extendPrices: null,
        threshold: null,
        background: null,
        systemId: null,
        typeId: null,
        userId: null,
        storeId: null,
        createUserId: null,
        dateCreated: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.templateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模板信息";
    },
    handleDesign(row) {
      console.log('come soon')
    },
    handleMaketag(row) {
      console.log('come soon')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const templateId = row.templateId || this.ids
      getTagtemplates(templateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模板信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.templateId != null) {
            updateTagtemplates(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTagtemplates(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.templateId || this.ids;
      this.$modal.confirm('是否确认删除模板信息编号为"' + templateIds + '"的数据项？').then(function() {
        return delTagtemplates(templateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tagtemplates/export', {
        ...this.queryParams
      }, `tagtemplates_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
